package com.itfreer.file;

import java.io.Serializable;

/**
 * 定义断点续传文件块信息
 */
public class FileSegmentInfo implements Cloneable, Serializable {
	
	private static final long serialVersionUID = 1L;

	/**
	 * 文件总长度
	 */
	private long fileLength;

	/**
	 * 文件块索引
	 * 从0开始
	 */
	private Integer segmentIndex;

	/**
	 * 文件块大小
	 * 单位为b
	 */
	private Integer segmentSize;
	
	/**
	 * 文件块内容
	 */
	private byte[] segmentContent;

	/**
	 * 文件块索引
	 * 从0开始
	 */
	public Integer getSegmentIndex() {
		return segmentIndex;
	}

	/**
	 * 文件块索引
	 */
	public void setSegmentIndex(Integer segmentIndex) {
		this.segmentIndex = segmentIndex;
	}

	/**
	 * 文件块大小
	 */
	public Integer getSegmentSize() {
		return segmentSize;
	}

	/**
	 * 文件块大小
	 * 单位为kb
	 */
	public void setSegmentSize(Integer segmentSize) {
		this.segmentSize = segmentSize;
	}

	/**
	 * 文件块内容
	 */
	public byte[] getSegmentContent() {
		return segmentContent;
	}

	/**
	 * 文件块内容
	 */
	public void setSegmentContent(byte[] segmentContent) {
		this.segmentContent = segmentContent;
	}
	
	/**
	 * 文件总长度
	 */
	public long getFileLength() {
		return fileLength;
	}

	/**
	 * 文件总长度
	 */
	public void setFileLength(long fileLength) {
		this.fileLength = fileLength;
	}

	/**
	 * 文件块总数
	 */
	public long getSegmentCount() {
		return (this.fileLength / this.segmentSize) + (this.fileLength % this.segmentSize > 0 ? 1 : 0);
	}

	/**
	 * 最否是最后文件块
	 */
	public boolean isLastSegment() {
		return this.segmentIndex == getSegmentCount() - 1;
	}
	
	@Override
	public Object clone() {
		FileSegmentInfo cloneObject = new FileSegmentInfo();
		cloneObject.fileLength = this.fileLength;
		cloneObject.segmentContent = this.segmentContent;
		cloneObject.segmentIndex = this.segmentIndex;
		cloneObject.segmentSize = this.segmentSize;
	    return cloneObject;
	}
}